package com.homedo.microservice.open.service.gateway.manager.web.convent;

import com.google.common.collect.Lists;
import com.homedo.common.util.Detect;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.resp.*;
import com.homedo.microservice.open.service.gateway.manager.web.bean.po.enhanced.DbSourceEnhanced;
import com.homedo.microservice.open.service.gateway.manager.web.bean.po.enhanced.ServiceGatewayEnhanced;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.po.enhanced.*;
import org.springframework.beans.BeanUtils;

import java.util.List;

public abstract class CommonConvent {

    public static List<ServiceGatewayAResp> convent2ServcieGatewayAResps(List<ServiceGatewayEnhanced> serviceGatewayEnhanceds) {
        List<ServiceGatewayAResp> serviceGatewayAResps = Lists.newArrayList();
        serviceGatewayEnhanceds.forEach(e -> {
            serviceGatewayAResps.add(convent2ServcieGatewayAResp(e));
        });
        return serviceGatewayAResps;
    }

    public static ServiceGatewayAResp convent2ServcieGatewayAResp(ServiceGatewayEnhanced serviceGatewayEnhanced) {
        ServiceGatewayAResp serviceGatewayAResp = new ServiceGatewayAResp();
        BeanUtils.copyProperties(serviceGatewayEnhanced, serviceGatewayAResp);

        return serviceGatewayAResp;
    }

    public static List<ZuulFilterAResp> convent2ZuulFilterAResps(List<ZuulFilterEnhanced> zuulFilterEnhanceds) {
        List<ZuulFilterAResp> zuulFilterAResps = Lists.newArrayList();
        zuulFilterEnhanceds.forEach(e -> {
            zuulFilterAResps.add(convent2ZuulFilterAResp(e));
        });
        return zuulFilterAResps;
    }

    public static ZuulFilterAResp convent2ZuulFilterAResp(ZuulFilterEnhanced zuulFilterEnhanced) {
        ZuulFilterAResp zuulFilterAResp = new ZuulFilterAResp();

        BeanUtils.copyProperties(zuulFilterEnhanced, zuulFilterAResp);
        if (zuulFilterEnhanced.getStatus() != null) {
            zuulFilterAResp.setStatusCode(zuulFilterEnhanced.getStatus().getCode());
            zuulFilterAResp.setStatusDesc(zuulFilterEnhanced.getStatus().getMsg());
        }

        if (zuulFilterEnhanced.getProgress() != null) {
            zuulFilterAResp.setProgressCode(zuulFilterEnhanced.getProgress().getCode());
            zuulFilterAResp.setProgressDesc(zuulFilterEnhanced.getProgress().getMsg());
        }

        return zuulFilterAResp;
    }

    public static DbSourceAResp convent2DbSourceAResp(DbSourceEnhanced dbSourceEnhanced) {
        DbSourceAResp dbSourceAResp = new DbSourceAResp();

        BeanUtils.copyProperties(dbSourceEnhanced, dbSourceAResp);
        if (Detect.notEmpty(dbSourceEnhanced.getPassword())) {
            dbSourceAResp.setPassword("******");
        }

        return dbSourceAResp;
    }

    public static List<ConfigAResp> convent2ConfigAResps(List<ConfigEnhanced> configEnhanceds) {
        List<ConfigAResp> configAResps = Lists.newArrayList();

        configEnhanceds.forEach(e -> configAResps.add(convent2ConfigAResp(e)));

        return configAResps;
    }

    public static ConfigAResp convent2ConfigAResp(ConfigEnhanced configEnhanced) {
        ConfigAResp configAResp = new ConfigAResp();

        BeanUtils.copyProperties(configEnhanced, configAResp);

        return configAResp;
    }


    public static List<RouteDomainAResp> convent2RouteDomainAResps(List<RouteDomainEnhanced> routeDomainEnhanceds) {
        List<RouteDomainAResp> routeDomainAResps = Lists.newArrayList();

        routeDomainEnhanceds.forEach(e -> routeDomainAResps.add(convent2RouteDomainAResp(e)));

        return routeDomainAResps;
    }

    public static RouteDomainAResp convent2RouteDomainAResp(RouteDomainEnhanced routeDomainEnhanced) {
        RouteDomainAResp routeDomainAResp = new RouteDomainAResp();

        BeanUtils.copyProperties(routeDomainEnhanced, routeDomainAResp);

        if (routeDomainEnhanced.getStatus() != null) {
            routeDomainAResp.setStatusCode(routeDomainEnhanced.getStatus().getCode());
            routeDomainAResp.setStatusDesc(routeDomainEnhanced.getStatus().getMsg());
        }

        if (Detect.notEmpty(routeDomainEnhanced.getRouteEnhanceds())) {
            routeDomainAResp.setRoutes(convent2RouteAResps(routeDomainEnhanced.getRouteEnhanceds()));
        }
        if (routeDomainAResp.getDomain() == null) {
            routeDomainAResp.setDomain("");
        }

        return routeDomainAResp;
    }

    public static List<RouteAResp> convent2RouteAResps(List<RouteEnhanced> routeEnhanceds) {
        List<RouteAResp> routeAResps = Lists.newArrayList();

        routeEnhanceds.forEach(e -> routeAResps.add(convent2RouteAResp(e)));

        return routeAResps;
    }

    public static RouteAResp convent2RouteAResp(RouteEnhanced routeEnhanced) {
        RouteAResp routeAResp = new RouteAResp();

        BeanUtils.copyProperties(routeEnhanced, routeAResp);
        if (routeEnhanced.getRouteServiceEnhanced() != null) {
            routeAResp.setRouteService(convent2RouteServiceAResp(routeEnhanced.getRouteServiceEnhanced()));
        }

        if (routeEnhanced.getStatus() != null) {
            routeAResp.setStatusCode(routeEnhanced.getStatus().getCode());
            routeAResp.setStatusDesc(routeEnhanced.getStatus().getMsg());
        }

        if (routeEnhanced.getRouteDomainEnhanced() != null) {
            routeAResp.setRouteDomain(convent2RouteDomainAResp(routeEnhanced.getRouteDomainEnhanced()));
        }

        if (Detect.notEmpty(routeEnhanced.getRouteVersionEnhanceds())) {
            routeAResp.setRouteVersions(convent2RouteVersionAResps(routeEnhanced.getRouteVersionEnhanceds()));
        } else {
            routeAResp.setRouteVersions(Lists.newArrayList());
        }

        return routeAResp;
    }

    public static List<RouteVersionAResp> convent2RouteVersionAResps(List<RouteVersionEnhanced> routeVersionEnhanceds) {
        List<RouteVersionAResp> routeVersionAResps = Lists.newArrayList();

        routeVersionEnhanceds.forEach(e -> routeVersionAResps.add(convent2RouteVersionAResp(e)));

        return routeVersionAResps;
    }

    public static RouteVersionAResp convent2RouteVersionAResp(RouteVersionEnhanced routeVersionEnhanced) {
        RouteVersionAResp routeVersionAResp = new RouteVersionAResp();

        BeanUtils.copyProperties(routeVersionEnhanced, routeVersionAResp);

        if (routeVersionEnhanced.getStatus() != null) {
            routeVersionAResp.setStatusCode(routeVersionEnhanced.getStatus().getCode());
            routeVersionAResp.setStatusDesc(routeVersionEnhanced.getStatus().getMsg());
        }
        if (routeVersionEnhanced.getRouteEnhanced() != null) {
            routeVersionAResp.setRoute(convent2RouteAResp(routeVersionEnhanced.getRouteEnhanced()));
        }
        if (routeVersionEnhanced.getRouteServiceEnhanced() != null) {
            routeVersionAResp.setRouteService(convent2RouteServiceAResp(routeVersionEnhanced.getRouteServiceEnhanced()));
        }

        return routeVersionAResp;
    }

    public static List<RouteServiceAResp> convent2RouteServiceAResps(List<RouteServiceEnhanced> routeServiceEnhanceds) {
        List<RouteServiceAResp> routeServiceAResps = Lists.newArrayList();

        routeServiceEnhanceds.forEach(e -> routeServiceAResps.add(convent2RouteServiceAResp(e)));

        return routeServiceAResps;
    }

    public static RouteServiceAResp convent2RouteServiceAResp(RouteServiceEnhanced routeServiceEnhanced) {
        RouteServiceAResp routeServiceAResp = new RouteServiceAResp();

        BeanUtils.copyProperties(routeServiceEnhanced, routeServiceAResp);

        if (routeServiceEnhanced.getStatus() != null) {
            routeServiceAResp.setStatusCode(routeServiceEnhanced.getStatus().getCode());
            routeServiceAResp.setStatusDesc(routeServiceEnhanced.getStatus().getMsg());
        }

        if (Detect.notEmpty(routeServiceEnhanced.getRouteServiceUrlEnhanceds())) {
            routeServiceAResp.setRouteServiceUrls(convent2RouteServiceUrlAResps(routeServiceEnhanced.getRouteServiceUrlEnhanceds()));
        } else {
            routeServiceAResp.setRouteServiceUrls(Lists.newArrayList());
        }

        if (Detect.notEmpty(routeServiceEnhanced.getAlertUserEnhanceds())) {
            routeServiceAResp.setAlertUsers(convent2AlertUserAResps(routeServiceEnhanced.getAlertUserEnhanceds()));
        } else {
            routeServiceAResp.setAlertUsers(Lists.newArrayList());
        }

        return routeServiceAResp;
    }

    public static List<RouteServiceUrlAResp> convent2RouteServiceUrlAResps(List<RouteServiceUrlEnhanced> routeServiceUrlEnhanceds) {
        List<RouteServiceUrlAResp> routeServiceUrlAResps = Lists.newArrayList();

        routeServiceUrlEnhanceds.forEach(e -> routeServiceUrlAResps.add(convent2RouteServiceUrlAResp(e)));

        return routeServiceUrlAResps;
    }

    public static RouteServiceUrlAResp convent2RouteServiceUrlAResp(RouteServiceUrlEnhanced routeServiceUrlEnhanced) {
        RouteServiceUrlAResp routeServiceUrlAResp = new RouteServiceUrlAResp();

        BeanUtils.copyProperties(routeServiceUrlEnhanced, routeServiceUrlAResp);

        if (routeServiceUrlEnhanced.getStatus() != null) {
            routeServiceUrlAResp.setStatusCode(routeServiceUrlEnhanced.getStatus().getCode());
            routeServiceUrlAResp.setStatusDesc(routeServiceUrlEnhanced.getStatus().getMsg());
        }

        if (routeServiceUrlEnhanced.getRouteServiceEnhanced() != null) {
            routeServiceUrlAResp.setRouteService(convent2RouteServiceAResp(routeServiceUrlEnhanced.getRouteServiceEnhanced()));
        }

        return routeServiceUrlAResp;
    }

    public static List<AlertUserAResp> convent2AlertUserAResps(List<AlertUserEnhanced> alertUserEnhanceds) {
        List<AlertUserAResp> alertUserAResps = Lists.newArrayList();

        alertUserEnhanceds.forEach(e -> alertUserAResps.add(convent2AlertUserAResp(e)));

        return alertUserAResps;
    }

    public static AlertUserAResp convent2AlertUserAResp(AlertUserEnhanced alertUserEnhanced) {
        AlertUserAResp alertUserAResp = new AlertUserAResp();

        BeanUtils.copyProperties(alertUserEnhanced, alertUserAResp);

        return alertUserAResp;
    }
}
